package com.ykt.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.interceptor.Seach;


public class Customer extends SeachModel<Customer>{

	private static final long serialVersionUID = 1L;
	public static final Customer dao = new Customer();
	
	public Customer getCustomer(String name,String phone){
		return dao.findFirst("select * from customer where username =? and phone =?",name,phone);
	}
	public Customer getCustomerByName(String name){
		return dao.findFirst("select * from customer where username =?",name);
	}
	public Customer getCustomerByPhone(String phone){
		return dao.findFirst("select * from customer where phone =?", phone);
	}
	
	public Customer getCustomerByPhone(String phone, int shopId){
		String sql = " select * from customer where phone =? ";
		if(shopId != 0){
			sql +=" and shop_id=? ";
			return dao.findFirst(sql, phone,shopId);
		}
		return dao.findFirst(sql, phone);
	}
	
	public Customer getCustomerByEmail(String email){
		return dao.findFirst("select * from customer where email =?",email);
	}
	public Customer checkCustomer(String name,String pwd){
		String sql="select * from customer where username =? and password=?";
		return findFirst(sql, name,pwd);
	}
	
	public Customer checkCustomer(String name,String pwd,int shopId){
		String sql="select * from customer where username =? and password=?";
		if(shopId != 0){
			sql +=" and shop_id=? ";
			return findFirst(sql, name,pwd,shopId);
		}
		return findFirst(sql, name,pwd);
	}
	
	//得到商户
	public Shop getShop(){
		return Shop.dao.findById(getInt("shop_id"));
	}
	//得到类别
	public CustomerType getCustomerType(){
		return CustomerType.dao.findById(getInt("customer_type_id"));
	}

	/**
	 * 一对多 得到所有订单
	 * @return
	 */
	public List<Order> getOrder(){
		return Order.dao.find("select * from orders where customer_id=?", getInt("customer_id"));
	}
	
	public Page<Customer> paginate(int pageNumber,int rows,Seach seach){
		String select="select a.*,s.name as shop_name,t.name as customer_type_name";
		String sqlExceptSelect = " from customer a left join shop s on a.shop_id = s.id left join customer_type t on a.customer_type_id = t.id where 1=1 ";
		return paginateBySelect(select, sqlExceptSelect, pageNumber, rows, seach);
	}
}
